package cn.iocoder.yudao.module.tms.controller.admin.attachmentinfo;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.tms.controller.admin.attachmentinfo.vo.*;
import cn.iocoder.yudao.module.tms.dal.dataobject.attachmentinfo.AttachmentInfoDO;
import cn.iocoder.yudao.module.tms.service.attachmentinfo.AttachmentInfoService;

@Tag(name = "管理后台 - 公司附件信息")
@RestController
@RequestMapping("/tms/attachment-info")
@Validated
public class AttachmentInfoController {

    @Resource
    private AttachmentInfoService attachmentInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建公司附件信息")
    @PreAuthorize("@ss.hasPermission('tms:attachment-info:create')")
    public CommonResult<Long> createAttachmentInfo(@Valid @RequestBody AttachmentInfoSaveReqVO createReqVO) {
        return success(attachmentInfoService.createAttachmentInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司附件信息")
    @PreAuthorize("@ss.hasPermission('tms:attachment-info:update')")
    public CommonResult<Boolean> updateAttachmentInfo(@Valid @RequestBody AttachmentInfoSaveReqVO updateReqVO) {
        attachmentInfoService.updateAttachmentInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司附件信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tms:attachment-info:delete')")
    public CommonResult<Boolean> deleteAttachmentInfo(@RequestParam("id") Long id) {
        attachmentInfoService.deleteAttachmentInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除公司附件信息")
                @PreAuthorize("@ss.hasPermission('tms:attachment-info:delete')")
    public CommonResult<Boolean> deleteAttachmentInfoList(@RequestParam("ids") List<Long> ids) {
        attachmentInfoService.deleteAttachmentInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司附件信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tms:attachment-info:query')")
    public CommonResult<AttachmentInfoRespVO> getAttachmentInfo(@RequestParam("id") Long id) {
        AttachmentInfoDO attachmentInfo = attachmentInfoService.getAttachmentInfo(id);
        return success(BeanUtils.toBean(attachmentInfo, AttachmentInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司附件信息分页")
    @PreAuthorize("@ss.hasPermission('tms:attachment-info:query')")
    public CommonResult<PageResult<AttachmentInfoRespVO>> getAttachmentInfoPage(@Valid AttachmentInfoPageReqVO pageReqVO) {
        PageResult<AttachmentInfoDO> pageResult = attachmentInfoService.getAttachmentInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttachmentInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司附件信息 Excel")
    @PreAuthorize("@ss.hasPermission('tms:attachment-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAttachmentInfoExcel(@Valid AttachmentInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttachmentInfoDO> list = attachmentInfoService.getAttachmentInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公司附件信息.xls", "数据", AttachmentInfoRespVO.class,
                        BeanUtils.toBean(list, AttachmentInfoRespVO.class));
    }

}